package com.xuetang9.t9.hotel.mapper;

import com.xuetang9.t9.hotel.beans.BillDetail;
import com.xuetang9.t9.hotel.mapper.condition.ICondition;
import com.xuetang9.t9.hotel.mapper.sql.impl.BillDetailMapperSql;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 账单明细
 * @author Pang
 */
@Repository
public interface IBillDetailMapper {

    /**
     * 新增账单明细表
     * @param billDetail
     * @return
     */
    @Insert("INSERT INTO g_billdetail(bill_id,arrive_id,bill_originalprice,bill_nowprice,bill_deposit,bill_reservationptice) " +
            "VALUES(#{billId},#{arriveId},#{billOriginalprice},#{billNowprice},#{billDeposit},#{billReservationptice})")
    int insert(BillDetail billDetail);

    /**
     * 根据入住记录表编号删除账单明细表
     * @param
     * @return
     */
    @Delete("DELETE FROM g_billdetail WHERE arrive_id = #{id}")
    int deleteByArriveId(@Param("id") Long arriveId);


    /**
     *根据账单明细表编号删除账单
     * @param id
     * @return
     */
    @Delete("DELETE FROM g_billdetail WHERE bill_id = #{id}")
    int deleteById(@Param("id")Long id);

    /**
     * 更新账单明细表
     * @param billDetail
     * @return
     */
    @Update("UPDATE g_billdetail SET bill_attribute=#{billAttribute},bill_originalprice=#{billOriginalprice},bill_nowprice=#{billNowprice},bill_deposit=#{billDeposit},bill_reservationptice=#{billReservationptice},bill_expensesum=#{billExpensesum} " +
            "WHERE bill_id=#{billId}")
    int update(BillDetail billDetail);


    /**
     * 查询条件查询所有账单明细表
     * @param condition
     * @return
     */
    @SelectProvider(value = BillDetailMapperSql.class,method = "selectByAll")
    List<BillDetail> selectByAll(@Param("key") ICondition condition);


    /**
     * 根据条件查询账单明细数量
     * @param condition
     * @return
     */
    @SelectProvider(value = BillDetailMapperSql.class,method = "selectByCount")
    Long selectByCount(@Param("key") ICondition condition);


    /**
     * 根据账单明细表id查询
     * @return
     */
    @Select("SELECT * FROM g_billdetail WHERE bill_id = #{id}")
    @Results(value = {
            @Result(column = "bill_id",property = "commodityExpenses",many = @Many(select = "com.xuetang9.t9.hotel.mapper.ICommodityExpenseMapper.selectByBillId",fetchType = FetchType.LAZY))
    })
    BillDetail selectById(@Param("id") Long id);

    /**
     * 根据入住记录表查询账单明细
     * @param id
     * @return
     */
    @Select("SELECT * FROM g_billdetail WHERE arrive_id=#{id}")
    BillDetail selectByArriveId(@Param("id") Long id);

}
